package com.googlecode.mailnet.math.distribution;

/**
 * @author Iskander Akishev
 */
class CompositeDistribution implements Distribution {
    private final Distribution x, y;
    private final Operation operation;

    CompositeDistribution(Distribution x, Distribution y, Operation operation) {
        this.x = x;
        this.y = y;
        this.operation = operation;
    }

    public double getValue() {
        return operation.apply(x.getValue(), y.getValue());
    }

    static enum Operation {
        PLUS {
            public double apply(double x, double y) {
                return x + y;
            }
        },
        MINUS {
            public double apply(double x, double y) {
                return x - y;
            }
        },
        MULTIPLY {
            public double apply(double x, double y) {
                return x * y;
            }
        },
        DIVIDE {
            public double apply(double x, double y) {
                return x / y;
            }
        },
        MIN {
            public double apply(double x, double y) {
                return Math.min(x, y);
            }
        },
        MAX {
            public double apply(double x, double y) {
                return Math.max(x, y);
            }
        },
        ;
        abstract double apply(double x, double y);
    }
}
